<template>
  <div class='container'>
    <el-dialog :title="title2" :visible="showDialog" @close="handleClose" @closed='onAddClose'>
      <el-form label-width="120px" :model="form" :rules="rules" ref="addRef">
        <el-form-item label="学科名称" prop="subjectName">
          <el-input width="50%" placeholder="请输入学科名称" v-model="form.subjectName" />
        </el-form-item>
        <el-form-item label="是否显示">
          <el-switch v-model="status" active-color="#13ce66" inactive-color="#ff4949">
          </el-switch>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="handleClose">取 消</el-button>
        <el-button type="primary" @click="handleSubmit()">确 定</el-button>
      </span>
    </el-dialog>

  </div>
</template>

<script>
import { add, update } from '../../api/hmmm//subjects'
// import { status } from '@/api/hmmm/constants'
export default {
  name: 'subjectsAdd',
  props: {
    showDialog: {
      type: Boolean,
      default: false
    },
    form: {
      type: Object,
      required: true
    }
    // subjectName: {
    //   type: String,
    //   required: true
    // }
    // simpleList: {
    //   type: Object,
    //   required: true
    // }
  },
  data () {
    return {
      title: '',
      // 状态：启用禁用
      status: true,
      // form: {

      //   subjectName: '',
      //   isFrontDisplay: '1'
      // },
      value: true,
      formDate: {
        subjectName: ''
      },
      queryBody: {
        subjectName: '',
        isFrontDisplay: null
      },
      rules: {
        subjectName: [
          { required: true, message: '请输入学科名称', trigger: 'blur' }
        ]
      }
    }
  },
  computed: {
    title2 () {
      return this.title1()
    }
  },
  methods: {
    title1 () {
      if (!this.form.id) {
        this.title = '新增学科'
        return this.title
      } else {
        this.title = '编辑学科'
        return this.title
      }
    },
    // 监听更改状态
    // changeStatus () {
    //   if (this.status) {
    //     this.form.isFrontDisplay = '1'
    //   } else {
    //     this.form.isFrontDisplay = '0'
    //   }
    // },
    // openAdd () {
    //   if (this.formList.id) {
    //     this.form = this.formList
    //   }
    // },
    onAddClose () {
      this.$refs.addRef.resetFields()
    },
    // 新增学科
    async handleSubmit () {
      // const { data: res } = await list(this.form)
      // console.log(res)
      // add({ subjectName: this.formDate.subjectName, isFrontDisplay: this.value }).then(data => {
      //   if (data.status === 200) {
      //     this.$parent.getList()
      //     this.$emit('toggle-box', false)
      //   }
      // })
      // const ret = await add({ subjectName: this.formDate.subjectName, isFrontDisplay: this.value })
      // console.log(this.form)

      if (!this.form.id) {
        const ret = await add(this.form)
        console.log(ret)
        this.$parent.getList()
        this.$emit('toggle-box', false)
        this.$emit('reset', this.form)
      } else {
        const ret2 = await update(this.form)
        // this.form = ret2.data
        console.log(ret2)
        this.$parent.getList()
        this.$emit('toggle-box', false)
        this.$emit('reset', this.form)
        // console.log(this.form)
      }
    },
    // 关闭弹窗
    handleClose () {
      this.$emit('update:show-dialog', false)
      this.$refs.addRef.resetFields()
    }
  }
}
</script>

<style scoped lang='less'></style>
